package com.happyass.service.impl;

import com.happyass.dao.BookingDao;
import com.happyass.dao.TicketDao;
import com.happyass.pojo.Booking;
import com.happyass.pojo.Ticket;
import com.happyass.service.BookinfoService;
import com.happyass.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class TicketServiceImpl implements TicketService {
    @Autowired
    TicketDao ticketDao;

    @Override
    public HashMap<String, Object> findAll(Ticket ticket, int page) {
        //查询
        if(ticket.getTicketName()=="") ticket.setTicketName(null);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("ticketId",match -> match.contains())
                .withMatcher("ticketName",ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("ticketPrice",ExampleMatcher.GenericPropertyMatchers.exact());
        Example<Ticket> example = Example.of(ticket,matcher);
        Pageable pageable = PageRequest.of(page,10);
        //分页对象
        Page<Ticket> ticketsPage =ticketDao.findAll(example,pageable);
        //当前页的集合
        List<Ticket> ticketsList = ticketsPage.getContent();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("ticketsList",ticketsList);
        //获得当前页
        map.put("nowPage",ticketsPage.getNumber()+1);
        //获得前一页
        if(page>0)
            map.put("lastPage",ticketsPage.getNumber()-1);
        else map.put("lastPage",0);
        //获得下一页
        if(page<ticketsPage.getTotalPages()-1)
            map.put("nextPage",ticketsPage.getNumber()+1);
        else map.put("nextPage",ticketsPage.getNumber());
        //获得总页码
        map.put("conPage",ticketsPage.getTotalPages());
        //获得总数据条数
        map.put("conEle",ticketsPage.getTotalElements());
        return  map;
    }

    @Override
    public void delDataTicket(int ticketId) {
        ticketDao.deleteById(ticketId);
    }

    @Override
    public void addDataTicket(Ticket ticket) {
        ticketDao.save(ticket);
    }

    @Override
    public Ticket upDataTicket(int ticketId) {
        return ticketDao.findById(ticketId).orElse(null);
    }

    @Override
    public List<Ticket> findAll() {
        return ticketDao.findAll();
    }

}
